package typejump.objects;

import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;

import typejump.ImageManager;

public class MessageBox extends GameObject {

	private static BufferedImage CORNER_SW = ImageManager.getImage("sign_corner.png");
	private static BufferedImage CORNER_NW = ImageManager.rotate90(ImageManager.getImage("sign_corner.png"), 1);
	private static BufferedImage CORNER_NE = ImageManager.rotate90(ImageManager.getImage("sign_corner.png"), 2);
	private static BufferedImage CORNER_SE = ImageManager.rotate90(ImageManager.getImage("sign_corner.png"), 3);
	
	private static BufferedImage INSIDE = ImageManager.getImage("sign_inside.png");

	private static BufferedImage EDGE_S = ImageManager.getImage("sign_edge.png");
	private static BufferedImage EDGE_W = ImageManager.rotate90(ImageManager.getImage("sign_edge.png"), 1);
	private static BufferedImage EDGE_N = ImageManager.rotate90(ImageManager.getImage("sign_edge.png"), 2);
	private static BufferedImage EDGE_E = ImageManager.rotate90(ImageManager.getImage("sign_edge.png"), 3);
	
	private String message;
	private Font font = new Font("sans-serif", Font.BOLD, 12);
	private FontMetrics fm;
	private BufferedImage back;
	private String lastmsg;
	
	public void setMessage(String message) {
		this.message = message;
	}
	
	@Override
	public String generateSpawnCommand() {
		StringBuffer sb = new StringBuffer(message.length()*3);
		for (int i = 0; i < message.length(); i++) {
			if (i > 0) sb.append("-");
			sb.append((int)message.charAt(i));
		}
		return sb.toString();
	}
	
	public static MessageBox createObject(String params) {
		MessageBox mb = new MessageBox();
		String[] parts = params.split("-");
		StringBuffer msg = new StringBuffer(parts.length);
		for (String s : parts)
			msg.append((char)Integer.parseInt(s));
		mb.message = msg.toString();
		return mb;
	}
	
	@Override
	public boolean paintEnabled() {
		return true;
	}

	@Override
	public void paint(Graphics2D gg) {
		if (message == null) 
			return;
		if (back == null || back.getWidth() != iw() || back.getHeight() != ih()
				|| (lastmsg == null && message != null) || !lastmsg.equals(message)) {
			
			back = new BufferedImage(iw(), ih(), BufferedImage.TYPE_INT_ARGB);
			Graphics2D g = back.createGraphics();
			g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
			
			g.drawImage(CORNER_NW, 0, 0, null);
			g.drawImage(CORNER_SW, 0, ih() - CORNER_SW.getHeight(), null);
			g.drawImage(CORNER_NE, iw() - CORNER_NE.getWidth(), 0, null);
			g.drawImage(CORNER_SE, iw() - CORNER_SE.getWidth(), ih()
					- CORNER_SE.getHeight(), null);

			g.drawImage(EDGE_N, CORNER_NW.getWidth(), 0,
					iw() - CORNER_NW.getWidth() - CORNER_NE.getWidth(),
					EDGE_N.getHeight(), null);
			g.drawImage(EDGE_S, CORNER_NW.getWidth(),
					ih() - EDGE_S.getHeight(), iw() - CORNER_NW.getWidth()
							- CORNER_NE.getWidth(), EDGE_N.getHeight(), null);
			g.drawImage(EDGE_W, 0, CORNER_NW.getHeight(), EDGE_W.getWidth(),
					ih() - CORNER_NW.getHeight() - CORNER_SW.getHeight(), null);
			g.drawImage(EDGE_E, iw() - EDGE_W.getWidth(),
					CORNER_NW.getHeight(), EDGE_W.getWidth(),
					ih() - CORNER_NW.getHeight() - CORNER_SW.getHeight(), null);

			g.drawImage(INSIDE, CORNER_NW.getWidth(), CORNER_NW.getHeight(),
					iw() - CORNER_NW.getWidth() - CORNER_SE.getWidth(), ih()
							- CORNER_NW.getHeight() - CORNER_SE.getHeight(),
					null);

			String message = this.message == null ? "" : this.message;
			
			String[] lines = message.split("\n");
			Font f = g.getFont();
			g.setFont(font);
			if (fm == null) 
				fm = g.getFontMetrics();
			
			int y = 4 + fm.getAscent();
			for (String line : lines) {
				g.drawString(line, iw()/2 - fm.stringWidth(line)/2, y);
				y += fm.getHeight();
			}
			
			g.setFont(f);
		}
		gg.drawImage(back, 0, 0, null);
	}

	@Override
	public void update(double time) {
		
	}
	
}
